package com.ruoyi.MtTakeaway.service.Impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.MtTakeaway.config.GetAccess_token;
import com.ruoyi.MtTakeaway.domain.Mthylj;
import com.ruoyi.MtTakeaway.mapper.MthyljMapper;
import com.ruoyi.MtTakeaway.service.IMthyljService;
import com.ruoyi.common.utils.SecurityUtils;
import okhttp3.*;
import org.apache.poi.poifs.filesystem.Entry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 美团Service业务层处理
 * 
 * @author 张
 * @date 2023-09-24
 */
@Service
public class MthyljServiceImpl implements IMthyljService
{
    private static final String VERSION = "2.0";
    private static final Integer PAGELEVEL = 2;
    @Resource
    private MthyljMapper mthyljMapper;
    //美团一级代理
    @Value("${mt.utmSource}")
    private String utmSource;
    //美团apptoken
    @Value("${mt.appIdToken}")
    private String appIdToken;
    @Value("${mt.promotionld}")
    private String promotionld;
    /**
     * 查询美团
     * 
     * @param id 美团主键
     * @return 美团
     */
    @Override
    public Mthylj selectMthyljById(Long id)
    {
        return mthyljMapper.selectMthyljById(id);
    }

    /**
     * 查询美团列表
     * 
     * @param mthylj 美团
     * @return 美团
     */
    @Override
    public List<Mthylj> selectMthyljList(Mthylj mthylj)
    {
        return mthyljMapper.selectMthyljList(mthylj);
    }

    /**
     * 新增美团
     * 
     * @param mthylj 美团
     * @return 结果
     */
    @Override
    public int insertMthylj(Mthylj mthylj, HttpServletResponse response)
    {
        //生成随机数订单id
        String requestId = RandomUtil.randomNumbers(32);
        //当前时间戳
        String timestamp = System.currentTimeMillis()+"";
        //鉴权token
        String accessToken = GetAccess_token.encryptHex(utmSource+timestamp, appIdToken);
        String url ="https://union.dianping.com/api/promotion/link?requestId="+requestId+"&utmSource="+utmSource+"&version="+VERSION+"&accessToken="+accessToken+"&timestamp="+timestamp;
        //获取到二级代理商的id
        String mtutmSource = SecurityUtils.getMtutmSource();
        String mtutmSourceId = GetAccess_token.encryptHex(mtutmSource, appIdToken);
        //活动id
        String activity= mthylj.getActivityid()+"";
        Map<String, Object> jsonData = new HashMap<>();
        jsonData.put("utmSource", utmSource);
        jsonData.put("utmMedium", mtutmSourceId);
        jsonData.put("activity", activity);
        jsonData.put("promotionId", promotionld);
        jsonData.put("pageLevel", PAGELEVEL);
        jsonData.put("demandQrInfo", true);

        JSONObject jsonObject = new JSONObject(jsonData);
        // 将JSON格式的数据输出
        String result2 = HttpRequest.post(url)
                .header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")//头信息，多个头信息多次调用此方法即可
                .header(Header.USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36")
                .header(Header.ACCEPT,"application/json, text/plain, */*")
                .header(Header.HOST,"union.dianping.com")
                .header(Header.CONNECTION,"keep-alive")
                .body(jsonObject.toString())//表单内容
                .execute().body();
        Mthylj mthylj1 = JSON.parseObject(JSONUtil.parseObj(result2).get("data")+"", Mthylj.class);
        mthylj1.setActivityid(mthylj.getActivityid());
        mthylj1.setActivityname(mthylj.getActivityname());
        JSONObject jsonObject001 = new JSONObject((Map) JSONUtil.parseObj(result2).get("data"));
        long beginTime = jsonObject001.getLong("beginTime");
        long endTime = jsonObject001.getLong("endTime");
//        String ruleInfo = jsonObject001.get("ruleInfo")+"";
        //        mthylj1.setRuleInfo(ruleInfo);
        mthylj1.setBeginTime(new Date(beginTime*1000));
        mthylj1.setEndTime(new Date(endTime*1000));

        return mthyljMapper.insertMthylj(mthylj1);
    }

    /**
     * 修改美团
     * 
     * @param mthylj 美团
     * @return 结果
     */
    @Override
    public int updateMthylj(Mthylj mthylj)
    {
        return mthyljMapper.updateMthylj(mthylj);
    }

    /**
     * 批量删除美团
     * 
     * @param ids 需要删除的美团主键
     * @return 结果
     */
    @Override
    public int deleteMthyljByIds(Long[] ids)
    {
        return mthyljMapper.deleteMthyljByIds(ids);
    }

    /**
     * 删除美团信息
     * 
     * @param id 美团主键
     * @return 结果
     */
    @Override
    public int deleteMthyljById(Long id)
    {
        return mthyljMapper.deleteMthyljById(id);
    }
}
